package com.yongqi.duocai.quartz;

import com.yongqi.duocai.entity.Order;
import com.yongqi.duocai.mapper.OrderMapper;
import com.yongqi.duocai.service.OrderService;
import com.yongqi.duocai.service.impl.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 1.描述（@desc）：
 * 2.作者（@author）：夏亮辉
 * 3.日期（@date）：2021年07月01  14:48
 * 4.版本（@version）：1.0.0
 * 5.版权：
 */
@Component
public class QuartzTask {

    @Autowired
    private OrderMapper orderMapper ;

   @Scheduled(cron = "0 0/15 * * * ? ")
   public void quartzClose(){
       // 查询所有订单
       List<Order> orders = orderMapper.selectList(null);
       LocalDateTime now = LocalDateTime.now();
       for (int i = 0; i< orders.size(); i++){
           Order o = orders.get(i);
           Duration duration = Duration.between(o.getCreateTime(), now);
           long res = duration.toMinutes();
           if(res > 30L && o.getState() == 1){
               o.setState(5);
               o.setEndTime(LocalDateTime.now());
               orderMapper.updateById(o);
           }else if(res > 21600L && o.getState() == 3){
               o.setState(4);
               o.setEndTime(LocalDateTime.now());
               orderMapper.updateById(o);
           }
       }
   }

}
